package com.wf.smartcomm.server.admin.action;

import java.util.HashMap;

import javax.annotation.Resource;

import org.json.JSONObject;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.ContextLoader;

import com.opensymphony.xwork2.ModelDriven;
import com.wf.smartcomm.server.apns.IApnsService;
import com.wf.smartcomm.server.apns.model.Payload;
import com.wf.smartcomm.server.bean.Decorate;
import com.wf.smartcomm.server.bean.House;
import com.wf.smartcomm.server.bean.User;
import com.wf.smartcomm.server.model.PushMessage;
import com.wf.smartcomm.server.push.Pusher;
import com.wf.smartcomm.server.service.DecorateService;
import com.wf.smartcomm.server.service.HouseService;
import com.wf.smartcomm.server.service.UserService;
import com.wf.smartcomm.server.util.ConstantUtils;
import com.wf.smartcomm.server.util.PushUtils;
import com.wf.smartcomm.server.util.StringUtil;

@Controller(value="adminDecorateAction")
@Scope("prototype")
public class DecorateAction extends BaseAction implements ModelDriven<Decorate>{

	private Decorate decorate = new Decorate();
	@Resource
	private DecorateService decorateServiceImpl;
	@Resource
	private HouseService houseServiceImpl;
	
	public void add() throws Exception{
		House house = houseServiceImpl.getById(request.getParameter("hid"));
		
		User user = house.getUser();
		if(user == null){
			printJsonData(403);
			return;
		}
		decorate.setUser(user);
		decorate.setHouse(house);
		decorate.setStatus(ConstantUtils.DECORATE_STATUS_UNCHECK);
		if (!StringUtil.isEmpty(String.valueOf(decorate.getBailMoney()))) {
			decorate.setBailStatus(ConstantUtils.DECORATE_BAIL_PAY);
		}else {
			decorate.setBailStatus(ConstantUtils.DECORATE_BAIL_UNPAY);
		}
		
		if (!StringUtil.isEmpty(decorate.getObjectId())) {
			decorateServiceImpl.update(decorate);
		}else {
			decorateServiceImpl.save(decorate);
		}
		printJsonData(200);
	}
	
	public void list() throws Exception{
		String aoData = request.getParameter("aoData");
		String sEcho = "";// 记录操作的次数  每次加1
		String iDisplayStart = "";// 起始
		String iDisplayLength = "";// size
		String sSearch = "";// 搜索的关键字
		
		JSONObject jo = new JSONObject(aoData);
		sEcho = jo.getString("sEcho");
		iDisplayStart = jo.getString("iDisplayStart");
		iDisplayLength = jo.getString("iDisplayLength");
		sSearch = jo.getString("sSearch");
		page.setBeginIndex(Integer.parseInt(iDisplayStart));
		page.setPageSize(Integer.parseInt(iDisplayLength));
		User manager = (User) session.getAttribute("loginUser");
		if (!StringUtil.isEmpty(sSearch)) {
			decorateServiceImpl.queryByKey(manager.getLoginVillage(),sSearch, page);
		}else {
			decorateServiceImpl.list(manager.getLoginVillage(),page);
		}
		
		int  initEcho = Integer.parseInt(sEcho)+1;
		int count = page.getTotalCount();
		HashMap<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("iTotalRecords", count);
		dataMap.put("sEcho",initEcho);
		dataMap.put("iTotalDisplayRecords", count);
		dataMap.put("aaData", page.getDataList());
		printJsonData(outGson.toJson(dataMap));
	}
	
	public String edit() throws Exception{
		decorate = decorateServiceImpl.get(decorate.getObjectId());
		request.setAttribute("decorate", decorate);
		return "edit";
	}
	
	public void check() throws Exception{
		decorateServiceImpl.check(decorate.getObjectId());
		printJsonData(200);
		ApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
		UserService userServiceImpl = (UserService) context.getBean("userServiceImpl");
		User user = decorate.getUser();
		if (user.getDeviceType().equals(ConstantUtils.DEVICE_TYPE_ANDROID)) {
			Pusher pusher = PushUtils.getInstance();
			PushMessage pMsg = new PushMessage();
			pMsg.setPushType(ConstantUtils.PUSH_TYPE_DECORATE_PASS);
			pMsg.setData(decorate.getHouse().getObjectId());
			pusher.push0x20Message(user.getPushId().getBytes(), outGson.toJson(pMsg).getBytes());
			pusher.close();
		} else if(user.getDeviceType().equals(ConstantUtils.DEVICE_TYPE_APPLE)){
			IApnsService service = PushUtils.getApnsService();
			Payload payload = new Payload();
			payload.setBadge(1);
			payload.setSound("default");
			payload.addParam("type", ConstantUtils.PUSH_TYPE_DECORATE_PASS);
			service.sendNotification(user.getAppleToken(), payload);
		}
	}
	
	public void end() throws Exception{
		decorateServiceImpl.end(decorate.getObjectId());
		printJsonData(200);
	}

	public void delete() throws Exception{
		decorateServiceImpl.delete(decorate);
		printJsonData(200);
	}
	
	@Override
	public Decorate getModel() {
		return decorate;
	}
	
}
